package convert

import (
	"errors"
	"math"
	"strconv"
)

// Float32ToInt 将 float32 转换为 int，检查溢出
func Float32ToInt(value float32) (int, error) {
	if value < float32(math.MinInt) || value > float32(math.MaxInt) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int(value), nil
}

// Float32ToInt8 将 float32 转换为 int8，检查溢出
func Float32ToInt8(value float32) (int8, error) {
	if value < float32(math.MinInt8) || value > float32(math.MaxInt8) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int8(value), nil
}

// Float32ToInt16 将 float32 转换为 int16，检查溢出
func Float32ToInt16(value float32) (int16, error) {
	if value < float32(math.MinInt16) || value > float32(math.MaxInt16) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int16(value), nil
}

// Float32ToInt32 将 float32 转换为 int32，检查溢出
func Float32ToInt32(value float32) (int32, error) {
	if value < float32(math.MinInt32) || value > float32(math.MaxInt32) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int32(value), nil
}

// Float32ToInt64 将 float32 转换为 int64，检查溢出
func Float32ToInt64(value float32) (int64, error) {
	if value < float32(math.MinInt64) || value > float32(math.MaxInt64) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return int64(value), nil
}

// Float32ToUint 将 float32 转换为 uint，检查溢出
func Float32ToUint(value float32) (uint, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint")
	}
	if value > float32(math.MaxUint) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint(value), nil
}

// Float32ToUint8 将 float32 转换为 uint8，检查溢出
func Float32ToUint8(value float32) (uint8, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint8")
	}
	if value > float32(math.MaxUint8) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint8(value), nil
}

// Float32ToUint16 将 float32 转换为 uint16，检查溢出
func Float32ToUint16(value float32) (uint16, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint16")
	}
	if value > float32(math.MaxUint16) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint16(value), nil
}

// Float32ToUint32 将 float32 转换为 uint32，检查溢出
func Float32ToUint32(value float32) (uint32, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint32")
	}
	if value > float32(math.MaxUint32) {
		return 0, errors.New("overflow occurred during conversion")
	}
	return uint32(value), nil
}

// Float32ToUint64 将 float32 转换为 uint64，检查溢出
func Float32ToUint64(value float32) (uint64, error) {
	if value < 0 {
		return 0, errors.New("negative value cannot be converted to uint64")
	}
	return uint64(value), nil
}

// Float32ToFloat64 将float32类型转换为float64类型，直接进行类型转换
func Float32ToFloat64(value float32) (float64, error) {
	// 直接将float32类型的值转换为float64类型
	return float64(value), nil
}

// Float32ToString 将 float32 转换为 string
func Float32ToString(value float32) string {
	return strconv.FormatFloat(float64(value), 'f', -1, 32)
}

// Float32ToBool 将 float32 转换为 bool
func Float32ToBool(value float32) bool {
	return value != 0.0
}
